﻿using System;
using Glue;
using Xunit;

namespace GlueExamples
{
    namespace BidirectionalMapping
    {
        public class DomainPerson
        {
            public String Name { get; set; }
        }

        public class GuiPerson
        {
            public String Name { get; set; }
        }

        public class Basic_bidirectional_mapping
        {
            private readonly DomainPerson domainPerson;
            private readonly GuiPerson guiPerson;
            private readonly Mapping<DomainPerson, GuiPerson> mapping;

            public Basic_bidirectional_mapping()
            {
                domainPerson = new DomainPerson();
                guiPerson = new GuiPerson();

                mapping = new Mapping<DomainPerson, GuiPerson>();
                mapping.Relate(domainperson => domainperson.Name, guiperson => guiperson.Name);
                // or you could do this: mapping.AutoRelateEqualNames();
            }

            [Fact]
            public void Should_map_TowardsRight()
            {
                domainPerson.Name = "Tore";
                mapping.Map(domainPerson, guiPerson);

                Assert.Equal("Tore",guiPerson.Name);
            }

            [Fact]
            public void Should_map_TowardsLeft()
            {
                guiPerson.Name = "Tore";
                mapping.Map(guiPerson, domainPerson);

                Assert.Equal("Tore", domainPerson.Name);
            }
        }

        public class Basic_unidirectional_mapping
        {
            private readonly DomainPerson domainPerson;
            private readonly GuiPerson guiPerson;
            private readonly Mapping<DomainPerson, GuiPerson> mapping;

            public Basic_unidirectional_mapping()
            {
                domainPerson = new DomainPerson();
                guiPerson = new GuiPerson();

                mapping = new Mapping<DomainPerson, GuiPerson>();
                mapping.RelateTowardsRight(domainperson => domainperson.Name, guiperson => guiperson.Name);
                // or you could do this:
                //  mapping.AutoRelateEqualNames().IgnoreTowardsRight(domainperson=>domainperson.Name,guiperson=>guiperson.Name);
            }

            [Fact]
            public void Should_map_TowardsRight()
            {
                domainPerson.Name = "Tore";
                mapping.Map(domainPerson, guiPerson);

                Assert.Equal("Tore", guiPerson.Name);
            }

            [Fact]
            public void Should_not_map_TowardsLeft()
            {
                guiPerson.Name = "Tore";
                domainPerson.Name = "Henning";
                mapping.Map(guiPerson, domainPerson);

                Assert.Equal("Tore", guiPerson.Name);
                Assert.Equal("Henning", domainPerson.Name);
            }
        }

    }
}
